const fs = require('fs');
const path = require('path');
let str = "";
let loop = false;
let speed = 1;
let lastValue = 0;
let waitTime = 0;
fs.readdir("./animFiles", function (err, files) {
    if (err) throw err;
    saveFile("")
    for (var i = 0; i < files.length; i++) {
        if (files[i].indexOf(".anim")) {
            let anim = fs.readFileSync(path.join(__dirname, "../animFiles/" + files[i]), "utf8");
            anim = JSON.parse(anim);
            writeFunc(`\r\r[================================${anim._name}===============================]`)
            writeFunc(`\r\rpublic ${anim._name}(node:any) {`)
            if (anim.wrapMode == 2) {
                loop = true;
            } else {
                loop = false;
            }
            speed = anim.speed;
            checkChildren(anim.curveData);
            writeFunc(`\r}`)
        }
    }
    saveFile(str);
});
function checkChildren(data) {
    if (data.paths) pathsFunc(data.paths);//子节
    // if (data.comps) compsFunc(data.comps);//序列帧动画
    if (data.props) propsFunc(data.props);//属性动画

}
function pathsFunc(data) {
    for (let key in data) {
        //console.error(key);
        // //console.log(data[key]);
        // if (data[key].comps) compsFunc(data[key].comps);//序列帧动画
        if (data[key].props) propsFunc(data[key].props, key);//属性动画
    }
}

function compsFunc(data) {

    if (data["cc.Sprite"]) {
        let dat = data["cc.Sprite"];
        if (dat.spriteFrame) spriteFunc(dat.spriteFrame);
    }

}
function propsFunc(data, key) {
    if (key) {
        writeFunc(`\r      cc.tween(cc.find(\"${key}\",node))`)
    } else {
        writeFunc(`\r      cc.tween(node)`)
    }
    let parallel = "";
    let dot = "";
    if (loop) {
        writeFunc(`\r      .repeatForever(\r      cc.tween()`);
    }
    if (Object.keys(data).length > 1) {
        writeFunc(`\r      .parallel(`);
        parallel = "cc.tween()";
        dot = ",";
    }
    if (data.position) {
        lastValue = 0;
        for (let i = 0; i < data.position.length; i++) {

            if (i >= 1) {
                if (data.position[0].frame > 0.001) {
                    waitTime = data.position[0].frame / speed;
                    waitTime=waitTime.toFixed(2)
                } else {
                    waitTime = 0;
                }
                if (data.position.length == 1) {
                    writeFunc(`\r      ${parallel}.delay(${waitTime}).delay(${waitTime}.by(${(data.position[i].frame / speed - waitTime).toFixed(2)}, { position: cc.v2(${data.position[i].value[0].toFixed(2) - data.position[i - 1].value[0].toFixed(2)},${data.position[i].value[1].toFixed(2) - data.position[i - 1].value[1].toFixed(2)})})${dot}`);
                } else
                    if (i == 1) {

                        writeFunc(`\r      ${parallel}.delay(${waitTime}).delay(${waitTime}).by(${(data.position[i].frame / speed - waitTime).toFixed(2)}, { position: cc.v2(${data.position[i].value[0].toFixed(2) - data.position[i - 1].value[0].toFixed(2)},${data.position[i].value[1].toFixed(2) - data.position[i - 1].value[1].toFixed(2)})})`);
                    } else
                        if (i == data.position.length - 1) {
                            writeFunc(`\r      .by(${(data.position[i].frame / speed - lastValue).toFixed(2)}, { position: cc.v2(${data.position[i].value[0].toFixed(2) - data.position[i - 1].value[0].toFixed(2)},${data.position[i].value[1].toFixed(2) - data.position[i - 1].value[1].toFixed(2)})})${dot}`);
                        }
                        else {
                            writeFunc(`\r      .by(${(data.position[i].frame / speed - lastValue).toFixed(2)}, { position: cc.v2(${data.position[i].value[0].toFixed(2) - data.position[i - 1].value[0].toFixed(2)},${data.position[i].value[1].toFixed(2) - data.position[i - 1].value[1].toFixed(2)})})`);
                        }
                lastValue = data.position[i].frame / speed;
            }
        }
    }
    if (data.x) {
        lastValue = 0;
        for (let i = 0; i < data.x.length; i++) {
            //console.log(data.x[i].value);
            //console.log(data.x[i].curve);
            if (i >= 1) {
                if (data.x[0].frame > 0.001) {
                    waitTime = data.x[0].frame;
                    waitTime=waitTime.toFixed(2)
                } else {
                    waitTime = 0;
                }
                if (data.x.length == 2) {
                    writeFunc(`\r      ${parallel}.delay(${waitTime}).by(${(data.x[i].frame / speed - waitTime).toFixed(2)}, { position: cc.v2(${data.x[i].value - data.x[i - 1].value.toFixed(2)},0)})${dot}`);
                } else
                    if (i == 1) {
                        writeFunc(`\r      ${parallel}.delay(${waitTime}).by(${(data.x[i].frame / speed - waitTime).toFixed(2)}, { position: cc.v2(${data.x[i].value - data.x[i - 1].value.toFixed(2)},0)})`);
                    } else
                        if (i == data.x.length - 1) {
                            writeFunc(`\r      .by(${(data.x[i].frame / speed - lastValue).toFixed(2)}, { position: cc.v2(${data.x[i].value - data.x[i - 1].value.toFixed(2)},0)})${dot}`);
                        }
                        else {
                            writeFunc(`\r      .by(${(data.x[i].frame / speed - lastValue).toFixed(2)}, { position: cc.v2(${data.x[i].value - data.x[i - 1].value.toFixed(2)},0)})`);
                        }
                lastValue = data.x[i].frame / speed;
            }
        }
    }//属性动画
    if (data.y) {
        lastValue = 0;
        for (let i = 0; i < data.y.length; i++) {
            if (i >= 1) {
                if (data.y[0].frame > 0.001) {
                    waitTime = data.y[0].frame / speed;
                    waitTime=waitTime.toFixed(2)
                } else {
                    waitTime = 0;
                }
                if (data.y.length == 2) {
                    writeFunc(`\r      ${parallel}.delay(${waitTime}).by(${(data.y[i].frame / speed - waitTime).toFixed(2)}, { position: cc.v2(0,${data.y[i].value.toFixed(2) - data.y[i - 1].value.toFixed(2)})})${dot}`);
                } else
                    if (i == 1) {
                        writeFunc(`\r      ${parallel}.delay(${waitTime}).by(${(data.y[i].frame / speed - waitTime).toFixed(2)}, { position: cc.v2(0,${data.y[i].value.toFixed(2) - data.y[i - 1].value.toFixed(2)})})`)
                    } else
                        if (i == data.y.length - 1) {
                            writeFunc(`\r      .by(${(data.y[i].frame / speed - lastValue).toFixed(2)}, { position: cc.v2(0,${data.y[i].value.toFixed(2) - data.y[i - 1].value.toFixed(2)})})${dot}`)
                        }
                        else {
                            writeFunc(`\r      .by(${(data.y[i].frame / speed - lastValue).toFixed(2)}, { position: cc.v2(0,${data.y[i].value.toFixed(2) - data.y[i - 1].value.toFixed(2)})})`)
                        }
                lastValue = data.y[i].frame / speed;
            }
        }
    }//属性动画
    if (data.angle) {
        lastValue = 0;
        for (let i = 0; i < data.angle.length; i++) {
            //console.log(data.angle[i].value);
            //console.log(data.angle[i].curve);
            if (data.angle[0].frame > 0.001) {
                waitTime = data.angle[0].frame / speed;
                waitTime=waitTime.toFixed(2)
            } else {
                waitTime = 0;
            }
            if (i == data.angle.length - 1) {
                writeFunc(`\r      .to(${(data.angle[i].frame / speed - lastValue).toFixed(2)}, { angle: ${data.angle[i].value.toFixed(2)}})${dot}`)
            }
            else if (i == 0) {
                writeFunc(`\r      ${parallel}.delay(${waitTime}).to(${(data.angle[i].frame / speed - waitTime).toFixed(2)}, { angle: ${data.angle[i].value.toFixed(2)}})`)
            } else {
                writeFunc(`\r      .to(${(data.angle[i].frame / speed - lastValue).toFixed(2)}, { angle: ${data.angle[i].value.toFixed(2)}})`)
            }
            lastValue=data.angle[i].frame/speed;
        }
    }//属性动画
    if (data.scale) {
        lastValue = 0;
        for (let i = 0; i < data.scale.length; i++) {
            //console.log(data.scale[i].value);
            //console.log(data.scale[i].curve);
            if (data.scale[0].frame > 0.001) {
                waitTime = data.scale[0].frame / speed;
                waitTime=waitTime.toFixed(2)
            } else {
                waitTime = 0;
            }
            if (i == 0) {
                writeFunc(`\r      ${parallel}.delay(${waitTime}).to(${(data.scale[i].frame / speed - waitTime).toFixed(2)},  { scaleX: ${data.scale[i].value.x.toFixed(2)},scaleY: ${data.scale[i].value.y.toFixed(2)} })`)
            } else
                if (i == data.scale.length - 1) {
                    writeFunc(`\r      .to(${(data.scale[i].frame / speed - lastValue).toFixed(2)}, { scaleX: ${data.scale[i].value.x.toFixed(2)},scaleY: ${data.scale[i].value.y.toFixed(2)} })${dot}`)
                }
                else {
                    writeFunc(`\r      .to(${(data.scale[i].frame / speed - lastValue).toFixed(2)},  { scaleX: ${data.scale[i].value.x.toFixed(2)},scaleY: ${data.scale[i].value.y.toFixed(2)} })`)
                }
               lastValue=data.scale[i].frame/speed;
        }
    }
    if (data.scaleX) {
        lastValue = 0;
        for (let i = 0; i < data.scaleX.length; i++) {
            //console.log(data.scaleX[i].value);
            //console.log(data.scaleX[i].curve);
            if (data.scaleX[0].frame > 0.001) {
                waitTime = data.scaleX[0].frame / speed;
                waitTime=waitTime.toFixed(2)
            } else {
                waitTime = 0;
            }
            if (i == 0) {
                writeFunc(`\r      ${parallel}.delay(${waitTime}).to(${(data.scaleX[i].frame / speed - waitTime).toFixed(2)}, { scaleX: ${data.scaleX[i].value.toFixed(2)} })`)
            } else
                if (i == data.scaleX.length - 1) {
                    writeFunc(`\r      .to(${(data.scaleX[i].frame / speed - lastValue).toFixed(2)}, { scaleX: ${data.scaleX[i].value.toFixed(2)} })${dot}`)
                }
                else {
                    writeFunc(`\r      .to(${(data.scaleX[i].frame / speed - lastValue).toFixed(2)}, { scaleX: ${data.scaleX[i].value.toFixed(2)} })`)
                }
              lastValue=data.scaleX[i].frame/speed;
        }
    }//属性动画
    if (data.scaleY) {
        lastValue = 0;
        for (let i = 0; i < data.scaleY.length; i++) {
            //console.log(data.scaleY[i].value);
            //console.log(data.scaleY[i].curve);
            if (data.scaleY[0].frame > 0.001) {
                waitTime = data.scaleY[0].frame / speed;
                waitTime=waitTime.toFixed(2)
            } else {
                waitTime = 0;
            }
            if (i == 0) {
                writeFunc(`\r      ${parallel}.delay(${waitTime}).to(${(data.scaleY[i].frame / speed - waitTime).toFixed(2)}, { scaleY: ${data.scaleY[i].value.toFixed(2)} })`)
            } else
                if (i == data.scaleY.length - 1) {
                    writeFunc(`\r      .to(${(data.scaleY[i].frame / speed - lastValue).toFixed(2)}, { scaleY: ${data.scaleY[i].value.toFixed(2)} })${dot}`)
                }
                else {
                    writeFunc(`\r      .to(${(data.scaleY[i].frame / speed - lastValue).toFixed(2)}, { scaleY: ${data.scaleY[i].value.toFixed(2)} })`)
                }
               lastValue=data.scaleY[i].frame/speed;
        }
    }//属性动画
    if (data.opacity) {
        lastValue = 0;
        for (let i = 0; i < data.opacity.length; i++) {
            //console.log(data.opacity[i].value);
            //console.log(data.opacity[i].curve);
            if (data.opacity[0].frame > 0.001) {
                waitTime = data.opacity[0].frame / speed;
                waitTime=waitTime.toFixed(2)
            } else {
                waitTime = 0;
            }
            if (i == 0) {
                writeFunc(`\r      ${parallel}.delay(${waitTime}).to(${(data.opacity[i].frame / speed - waitTime).toFixed(2)}, { opacity: ${data.opacity[i].value.toFixed(2)} })`)
            } else
                if (i == data.opacity.length - 1) {
                    writeFunc(`\r      .to(${(data.opacity[i].frame / speed - lastValue).toFixed(2)}, { opacity: ${data.opacity[i].value.toFixed(2)} })${dot}`)
                }
                else {
                    writeFunc(`\r      .to(${(data.opacity[i].frame / speed - lastValue).toFixed(2)}, { opacity: ${data.opacity[i].value.toFixed(2)} })`)
                }
               lastValue=data.opacity[i].frame/speed;
        }
    }//属性动画
    if (loop) {
        writeFunc(`\r      )`);
    }
    if (Object.keys(data).length > 1) {
        writeFunc(`\r      )\r      .start();`);
    } else {
        writeFunc(`\r      .start();`);
    }
}

function spriteFunc(data) {
    //console.log(data)
}

function saveFile(data) {
    fs.appendFile('./animscript.ts', data, 'utf8', function (err) {
        if (err) {
            //console.log(err);
        }
    });
}

function writeFunc(data) {
    str += data;
}
